package net.learn2develop.view;


import net.learn2develop.DB.DatabaseHelper;
import net.learn2develop.GPS.PlaceInfo;
import net.learn2develop.GPS.R;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.maps.OverlayItem;

/**
 * A view representing a MapView marker information balloon.
 * <p>
 * This class has a number of Android resource dependencies:
 * <ul>
 * <li>drawable/balloon_overlay_bg_selector.xml</li>
 * <li>drawable/balloon_overlay_close.png</li>
 * <li>drawable/balloon_overlay_focused.9.png</li>
 * <li>drawable/balloon_overlay_unfocused.9.png</li>
 * <li>layout/balloon_map_overlay.xml</li>
 * </ul>
 * </p>
 * 
 * 
 *
 */
public class BalloonOverlayView extends FrameLayout {

	private LinearLayout layout;
	private TextView title;
	private TextView snippet;
	private View v;
	private SQLiteDatabase db;
	private CheckBox starCheckBox;
	
	/**
	 * Create a new BalloonOverlayView.
	 * 
	 * @param context - The activity context.
	 * @param balloonBottomOffset - The bottom padding (in pixels) to be applied
	 * when rendering this view.
	 * @param placeId - id`s place from Places Table.
	 */
	public BalloonOverlayView(Context context, int balloonBottomOffset, final int placeId) {

		super(context);
		setPadding(10, 0, 10, balloonBottomOffset);
		layout = new LinearLayout(context);
		layout.setVisibility(VISIBLE);

		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		v = inflater.inflate(R.layout.balloon_overlay, layout);
		title = (TextView) v.findViewById(R.id.balloon_item_title);
		snippet = (TextView) v.findViewById(R.id.balloon_item_snippet);
		
		checkFavorite(context, placeId);
		setSeeMoreButton(placeId);
		
		ImageView close = (ImageView) v.findViewById(R.id.close_img_button);
        close.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				layout.setVisibility(GONE);
			}
		});
		FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		params.gravity = Gravity.NO_GRAVITY;
		addView(layout, params);
	}
	
	public void checkFavorite(Context context, final int placeId) {
		//Favorite's Star in Balloon
		starCheckBox = (CheckBox) v.findViewById(R.id.checkbox_id);
		db = (new DatabaseHelper(context)).getWritableDatabase();
		Cursor cursor = db.rawQuery("SELECT favorite FROM places WHERE _id LIKE ?",new String[]{""+placeId});
		if(cursor.moveToFirst()){
			do{
				String val = cursor.getString(cursor.getColumnIndex("favorite"));
				int valor = Integer.valueOf(val);
				if(valor ==1){
					starCheckBox.setChecked(true);
				}else{
					starCheckBox.setChecked(false);
				}
			} while (cursor.moveToNext());
		}
		if (cursor != null && !cursor.isClosed()) {
	         cursor.close();
		}
		starCheckBox.setOnClickListener(new OnClickListener() {
	             public void onClick(View v) {
	            	 if(starCheckBox.isChecked()){
	            		 db.execSQL("UPDATE places SET favorite = 1 WHERE _id = "+placeId);
	            	 }else{
	 					db.execSQL("UPDATE places SET favorite = 0 WHERE _id = "+placeId);
	            	 }
	             }
	         });
	}

	/**
	 * Sets the action's button. 
	 * 
	 * @param placeId - Id place which allows to select a place from Place table.
	 */
	public void setSeeMoreButton(final int placeId){
			Button seeMoreButton = (Button) v.findViewById(R.id.button_seemore);
			seeMoreButton.setOnClickListener(new OnClickListener() {
 	             public void onClick(View v) {
					Intent intent = new Intent(getContext(), PlaceInfo.class);
					intent.putExtra("PLACE_ID", placeId);
					layout.setVisibility(GONE);
					getContext().startActivity(intent);
 	             }
 	         });	
	}
	
	
	/**
	 * Sets the view data from a given overlay item.
	 * 
	 * @param item - The overlay item containing the relevant view data 
	 * (title and snippet). 
	 */
	public void setData(OverlayItem item) {
		
		layout.setVisibility(VISIBLE);
		if (item.getTitle() != null) {
			title.setVisibility(VISIBLE);
			title.setText(item.getTitle());
		} else {
			title.setVisibility(GONE);
		}
		if (item.getSnippet() != null) {
			snippet.setVisibility(VISIBLE);
			snippet.setText(item.getSnippet());
		} else {
			snippet.setVisibility(GONE);
		}
		
	}
	
	/**
	 * Sets the view picture from a given overlay item.
	 * 
	 * @param picture - The overlay item containing the balloon's picture 
	 */
	public void setImage(String picture) {
		//image point
		TextView image =(TextView)v.findViewById(R.id.ImageView01);
        //image.setImageDrawable(pic);
		
		//agregado para HTML
		image.setText(Html.fromHtml(picture, new HttpImageGetter(), null));
	    //mTextSample.setText(Html.fromHtml(text));
		image.setMovementMethod(LinkMovementMethod.getInstance());
	}
}
